﻿#include "SerialPort.h"

SerialPort::SerialPort(Serial serial, QObject *parent) : MyObject(parent)
{
    this->isOpen = false;
    this->serialPort = nullptr;
    this->serial = serial;
}

void SerialPort::Start(){
    MyLog::Info(QString("SerialPort::Start"));

    //connect(globalData, SIGNAL(Update()), this, SLOT(CreateSerial()));
    connect(this, SIGNAL(SerialSend(QByteArray)), this, SLOT(SerialSendData(QByteArray)));

    timer = new QTimer;
    connect(timer, SIGNAL(timeout()), this, SLOT(OnTimer()));
    timer->start(5000);
    this->OnTimer();
}

void SerialPort::Stop(){
    timer->stop();
    serialPort->close();
    emit Finished();
}

void SerialPort::OnTimer(){
    if(serialPort == nullptr || !serialPort->isOpen()){
        this->CreateSerial();
    }
}

void SerialPort::CreateSerial(){//设置串口
    if(serialPort != nullptr) {//清空
        serialPort->close();
        serialPort->deleteLater();
        serialPort = nullptr;
    }

    serialPort = new QSerialPort();
    connect(serialPort, SIGNAL(readyRead()), this, SLOT(SerialReadData()));

    serialPort->setPortName(serial.serialName);//串口号
    serialPort->setBaudRate(serial.baudRate);//波特率
    serialPort->setDataBits(serial.GetDataBit());//数据位
    serialPort->setParity(serial.GetParity());//校验位
    serialPort->setStopBits(serial.GetStopBit());//停止位
    serialPort->setFlowControl(QSerialPort::NoFlowControl);//设置流控制
    isOpen = serialPort->open(QIODevice::ReadWrite);//读写打开串口

    globalData->serialOpen = isOpen;   
    LightStyle();
    if(isOpen){
        MyLog::Info("串口打开成功");
    }else{
        MyLog::Info("串口打开失败");
        qDebug()<<serialPort->error();

    }
}

void SerialPort::SerialReadData(){//接收数据
    QByteArray dataArray = serialPort->readAll();
    readData.append(dataArray);
    //MyLog::Info("收到数据：" + MyHelper::ToHexString(dataArray));
}

void SerialPort::SerialSendData(QByteArray data){//发送数据
    if(isOpen){
        serialPort->write(data.data(), data.length());
        serialPort->flush();//放入缓冲区
        //MyLog::Info("发送数据：" + MyHelper::ToHexString(data));
    }
}

bool SerialPort::SendData(QByteArray dataArray, QString retCheck){
    //mutex.lock();

    bool b = false;
    if(isOpen){

        readData.clear();

        if(QThread::currentThread() == this->thread()){
            this->SerialSendData(dataArray);
            serialPort->waitForReadyRead(200);//阻塞调用，知道有新的数据可读取
        }else{
            emit SerialSend(dataArray);
            QThread::msleep(200);
        }
        if(readData.length() > 3){
            if((uint8_t)readData.at(0) == 0xEE
                    && (uint8_t)readData.at(1) == 0xFF
                    && (uint8_t)readData.at(2) == readData.length() - 3)
            {
                b = true;
            }
        }

        if(b) emit SerialRead(readData);
        readData.clear();
    }

    //mutex.unlock();
    return b;
}

void SerialPort::LightStyle()
{
   if(serial.serialName==globalData->equList.at(0)->serialName)
   {
       globalData->Light_1=isOpen;
   }
   else if(serial.serialName==globalData->equList.at(1)->serialName)
   {
       globalData->Light_2=isOpen;
   }
   else if(serial.serialName==globalData->equList.at(2)->serialName)
   {
       globalData->Light_3=isOpen;
   }
   else if(serial.serialName==globalData->equList.at(3)->serialName)
   {
       globalData->Light_4=isOpen;
   }
}
